package delta;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.regex.Pattern;

import delta.Delta.Outputter;
import delta.Delta.DirOutputter;
import delta.Delta.ZipOutputter;

public class Main {
	public static void main(String[] args) throws IOException {
		String pathSeparator = System.getProperty("delta.path.separator", System.getProperty("path.separator"));
		String path0 = System.getProperty("path0");
		String path1 = System.getProperty("path1");
		String type = System.getProperty("output.type");
		String patch = System.getProperty("patch");
		Delta delta = new Delta();
		delta.delta(toOutputter(type),
				toFiles(path0, pathSeparator, new ArrayList<File>()),
				toFiles(path1, pathSeparator, new ArrayList<File>()),
				new File(patch));
	}

	static Outputter toOutputter(String type) {
		if ("zip".equals(type)) {
			return new ZipOutputter();
		} else if ("dir".equals(type)) {
			return new DirOutputter();
		} else {
			throw new RuntimeException("output.type = <" + type + ">");
		}
	}

	static <C extends Collection<File>> C toFiles(String path, String sep, C collection) {
		for (String p : path.split(Pattern.quote(sep))) {
			collection.add(new File(p));
		}
		return collection;
	}
}
